﻿using System.Windows.Controls;
using Microsoft.Web.WebView2.Core;
using Microsoft.Win32;

namespace PdfReadMod.Views
{
    /// <summary>
    /// Interaction logic for PdfReadMain
    /// </summary>
    public partial class PdfReadMain : UserControl
    {
        public PdfReadMain()
        {
            InitializeComponent();
        }

        private void BtOpenPdf_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var opf = new OpenFileDialog();
            opf.Filter = "PDF 文件|*.PDF";
            opf.Multiselect = false;
            if (opf.ShowDialog() == true )
            {
                var pdfFile = opf.FileName;
                webview2.CoreWebView2.Navigate(pdfFile);
            }
        }

        private async void webview2_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //允许从文件URI引用其他本地文件
            var options = new CoreWebView2EnvironmentOptions("--allow-file-access-from-files");
            var environment = await CoreWebView2Environment.CreateAsync(options: options);
            await webview2.EnsureCoreWebView2Async(environment);
            //webview2.CoreWebView2.Settings.AreDevToolsEnabled = false;
            //webview2.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
            if (this.webview2.IsLoaded)
            {
                this.webview2.CoreWebView2.ProcessFailed += CoreWebView2_ProcessFailed;
                this.webview2.CoreWebView2.Environment.BrowserProcessExited += Environment_BrowserProcessExited;
            }
            //webview2.CoreWebView2.Settings.HiddenPdfToolbarItems = CoreWebView2PdfToolbarItems.Bookmarks
            //| CoreWebView2PdfToolbarItems.Search
            //| CoreWebView2PdfToolbarItems.PageSelector;
        }

         private void Environment_BrowserProcessExited(object? sender, CoreWebView2BrowserProcessExitedEventArgs e)
         {
         }
        
         private void CoreWebView2_ProcessFailed(object? sender, Microsoft.Web.WebView2.Core.CoreWebView2ProcessFailedEventArgs e)
         {
         }
    }
}
